using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TreeGenerator;
using System.Drawing;
using System.Web.Caching;
using System.Xml;

public partial class ShowTree : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    /// <summary>
    /// Generates tree data randomely on the fly.
    /// In real life this function would be used to get the data from the DB.
    /// </summary>
    /// <param name="topID"></param>
    /// <returns></returns>
    private TreeGenerator.TreeData.TreeDataTableDataTable GetDT(string topID)
    {
        TreeGenerator.TreeData.TreeDataTableDataTable Result = new TreeData.TreeDataTableDataTable();
        //add the parent
        Result.AddTreeDataTableRow(topID, "", topID, "");
       
        Random r = new Random();
        int childCount = r.Next(8);
        string childName;
        for(int i=0;i<=childCount;i++)
        {
            //randomely add some children
            childName = topID + "-" + i.ToString();
            Result.AddTreeDataTableRow(childName, topID, childName, "");
            int childChildCount = r.Next(10);
            string childChildName;
            for (int j = 0; j <= childChildCount; j++)
            {
                //randomely add some children
                childChildName = childName + "-" + j.ToString();
                Result.AddTreeDataTableRow(childChildName, childName, childChildName, "");
            }
        }
        return Result;
    }
    
    /// <summary>
    /// Show the Tree based on what the user clicked
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnShowBOM_Click(object sender, EventArgs e)
    {
        if (txtItem.Text.Trim().Length > 0)
        {
            GenerateChart(txtItem.Text);
        }
    }
    /// <summary>
    /// This is called based on where the user clicked on the imagemap
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void impBOM_Click(object sender, ImageMapEventArgs e)
    {
        GenerateChart(e.PostBackValue);
    }
    /// <summary>
    /// Generate the chart image and save it in the cache, to be used by the image.aspx
    /// </summary>
    private void GenerateChart(string mItem)
    {
        //crate the chart object
         TreeGenerator.TreeBuilder currentBOM = new TreeGenerator.TreeBuilder(GetDT(mItem));
        
        //specify visual properties
        currentBOM.HorizontalSpace = 15;
        currentBOM.VerticalSpace = 15;
        currentBOM.FontSize = 7;
        currentBOM.BoxHeight = 80;
        currentBOM.BoxWidth = 80;
        currentBOM.LineColor = System.Drawing.ColorTranslator.FromHtml("#BFBFC9");
        currentBOM.FontColor = System.Drawing.ColorTranslator.FromHtml("#3B4164");
        System.Drawing.Image OC =
           System.Drawing.Image.FromStream(currentBOM.GenerateTree(mItem,
              System.Drawing.Imaging.ImageFormat.Bmp));

        string nameOfImage = Session.SessionID + "CurrentImage";
        //save in the cache, to be used by the page creating the image
        Cache.Insert(nameOfImage, OC, null, DateTime.MaxValue, TimeSpan.FromMinutes(1), CacheItemPriority.NotRemovable, null);
        //update the item image map
        //supply the image name by querystring
        impBOM.ImageUrl = "~/OrgChartImage.aspx?ID=" + nameOfImage;
        //remove all hotspots
        impBOM.HotSpots.Clear();
        //regenerate the hotspots.
        foreach (XmlNode oNode in currentBOM.xmlTree.SelectNodes("//Node"))
        {
            RectangleHotSpot RectHS = new RectangleHotSpot();
            RectHS.HotSpotMode = HotSpotMode.PostBack;
            Rectangle currentRect = currentBOM.getRectangleFromNode(oNode);
            RectHS.Top = currentRect.Top;
            RectHS.Bottom = currentRect.Bottom;
            RectHS.Left = currentRect.Left;
            RectHS.Right = currentRect.Right;
            RectHS.PostBackValue = oNode.Attributes["nodeID"].InnerText;
            RectHS.AlternateText = oNode.Attributes["nodeDescription"].InnerText;
            impBOM.HotSpots.Add(RectHS);
            //SelectedEmployee = EmpRec.EmployeeData.EmployeeName;

        }
    }

    
}
